import axios from "axios";
const request = axios.create({
	timeout: 10000,
	// baseURL: "/api" // 统一接口前缀
});
// 请求拦截器
request.interceptors.request.use(config => {
	// token
	const token = localStorage.getItem("token");
	// const token = Store.state.token
	const headers = {};
	if (token) {
		// 请求头携带token
		headers.authorization = token;
	}
	return {
		...config,
		headers,
	};
});
// 响应拦截器
request.interceptors.response.use(
	response => {
		return response.data;
	},
	err => {
		switch (err.response.status) {
			case 401:
				alert("没权限");
				// router.push('/login') // 重定向到登录
				break;
			case 403:
				alert("权限过期");
				break;
			case 404:
				alert("接口不存在");
				break;
		}
		return Promise.reject(err);
	}
);

export default request;
